#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简单的LLM分析器使用示例
演示如何使用修改后的LLM分析器处理低置信度异常
"""

import asyncio
import os
import sys

# 添加项目路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from agent_mcp.llm.llm_analyzer import LLMAnalyzer

async def simple_example():
    """简单的使用示例"""
    
    print("=== 简单LLM分析器示例 ===")
    
    # 初始化LLM分析器
    llm_analyzer = LLMAnalyzer(confidence_threshold=0.8)
    
    # 创建Ansible目录
    ansible_dir = "agent-mcp/Ansible"
    os.makedirs(ansible_dir, exist_ok=True)
    
    # 示例异常数据（低置信度）
    anomaly_data = {
        "anomaly_id": "simple_test_001",
        "issue_type": "cpu_usage",
        "root_cause": "potential cpu usage spike",
        "severity": 6,
        "contributing_factors": ["high load", "possible memory leak"],
        "evidence": ["cpu usage increased", "response time degraded"]
    }
    
    print(f"异常ID: {anomaly_data['anomaly_id']}")
    print(f"问题类型: {anomaly_data['issue_type']}")
    print(f"根本原因: {anomaly_data['root_cause']}")
    
    # 获取置信度信息
    confidence_info = llm_analyzer.get_confidence_info(anomaly_data)
    print(f"置信度分数: {confidence_info['confidence_score']:.3f}")
    print(f"置信度等级: {confidence_info['confidence_level']}")
    print(f"阈值: {confidence_info['threshold']}")
    
    # 处理异常（假设置信度已确认低于阈值）
    print("\n开始LLM分析...")
    try:
        result = await llm_analyzer.process_anomaly(anomaly_data, ansible_dir)
        
        if result['success']:
            print("✅ LLM分析处理成功!")
            print(f"   异常类型: {result.get('anomaly_type', 'N/A')}")
            print(f"   生成的脚本: {result.get('generated_script', 'N/A')}")
            print(f"   处理方式: {result.get('processing_method', 'N/A')}")
        else:
            print(f"❌ LLM分析处理失败: {result.get('error', '未知错误')}")
            
    except Exception as e:
        print(f"❌ 处理异常时出错: {e}")
    
    print("\n=== 示例完成 ===")

if __name__ == "__main__":
    asyncio.run(simple_example()) 